<?php
/***************************************************************************
 *                              admin_arcade.php
 *                            -------------------
 *   begin                : Mardi 13 Avril 2004
 *   email                : giefca@hotmail.com
 *
 *
 ***************************************************************************/

if ( !defined('IN_QUOTE') && !defined('IN_ADMIN') )
{
	die('Hacking attempt');
	exit;
}

if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['News']['News_Gestion'] = './admin.php?mode=news_gestion';
	return;
} else {	
/*	if ($news_id_send && $mode_news == 'delete')
	{
		if (!$group_info['can_del_news'])
		{
			error_message($lang['information'], $lang['cant_delete_news'], ADMIN_URL, '?mode=news_gestion');		
		} else {
			$sql = 'DELETE FROM ' . NEWS_TABLE . '
								WHERE news_id = ' . $news_id_send;
			if( !($res = $db->query($sql)) )
			{
				message_die(__LINE__, __FILE__, $sql);
			} else {
				error_message($lang['information'], $lang['good_del_news'], ADMIN_URL, '?mode=news_gestion');
			}
		}
	}
	else if ($news_id_send && $mode_news == 'edit')
	{
		if (!$group_info['can_edit_news'])
		{
			error_message($lang['information'], $lang['cant_edit_news'], ADMIN_URL, '?mode=news_gestion');
		} else {
			$news_subject = request_var('news_subject');
			$news_texte = request_var('news_texte', TYPE_HTML);
			$news_image = request_var('news_image');
			if ($submit && (!$news_subject || !$news_texte))
			{
				error_message($lang['information'], $lang['no_subject_news'], ADMIN_URL, '?mode=news_gestion&mode_news=create');
			}
			else if ($news_id_send && $submit)
			{
				$user_id = $userdata['user_id'];
				$time = time();
				$sql = 'UPDATE ' . NEWS_TABLE . " SET
									news_subject = '$news_subject',
									news_texte = '$news_texte',
									news_image = '$news_image'
									WHERE news_id = '$news_id_send'";
				if( !($db->query($sql)) )
				{
					message_die(__LINE__, __FILE__, $sql);
				} else {
					error_message($lang['information'], $lang['good_edit_news'], ADMIN_URL, '?mode=news_gestion');
				}
			}
			else 
			{
				$sql = 'SELECT * FROM ' . NEWS_TABLE . '
									WHERE news_id = ' . $news_id_send;
				if( !($res = $db->query($sql)) )
				{
					message_die(__LINE__, __FILE__, $sql);
				} else {	
					while($array = $db->fetcharray($res))
					{
						$news_info = $array;
					}
					include('./includes/fckeditor/fckeditor.php');
					$FCKeditor = new FCKeditor('news_texte');
					$FCKeditor->Height = '500px';
					$FCKeditor->BasePath = './includes/fckeditor/';
					$FCKeditor->Value = $news_info['news_texte'];
					
					$template->assign('FCKEDITOR', $FCKeditor->CreateHtml());
					$template->assign('NEWS_SUBJECTS', $news_info['news_subject']);
					$template->assign('U_SEND', './' . ADMIN_PAGE . '.php?mode=news_gestion&mode_news=edit&news_id=' . $news_id_send);
				
					$template->display('admin/create_edit_news.tpl');
				}
			}
		}
	}
	else if ($mode_news == 'create')
	{
		if (!$group_info['can_add_news'])
		{
			error_message($lang['information'], $lang['cant_add_news'], ADMIN_URL, '?mode=news_gestion');
		} else {
			$news_subject = request_var('news_subject');
			$news_texte = request_var('news_texte');
			$news_image = request_var('news_image');
			if ($submit && (!$news_subject || !$news_texte))
			{
				error_message($lang['information'], $lang['no_subject_news'], ADMIN_URL, '?mode=news_gestion&mode_news=create');
			} 
			else if ($submit)
			{
				$user_id = ($userdata['user_login']) ? $userdata['user_id'] : '-1';
				$time = time();
				$sql = 'INSERT INTO ' . NEWS_TABLE . "(news_subject, news_texte, news_image, news_user_id, news_time)
									VALUES ('$news_subject', '$news_texte', '$news_image', '$user_id', '$time')";
				if( !($db->query($sql)) )
				{
					message_die(__LINE__, __FILE__, $sql);
				} else {
					error_message($lang['information'], $lang['good_add_news'], ADMIN_URL, '?mode=news_gestion');
				}
			}
			else 
			{
				include('./includes/fckeditor/fckeditor.php');
				$FCKeditor = new FCKeditor('news_texte');
				$FCKeditor->Height = '500px';
				$FCKeditor->BasePath = './includes/fckeditor/';
				
				$template->assign('FCKEDITOR', $FCKeditor->CreateHtml());
				$template->assign('U_SEND', './' . ADMIN_PAGE . '.php?mode=news_gestion&mode_news=create');
				
				$template->display('admin/create_edit_news.tpl');
			}
		}
	}*/
	
	function dysplay_news()
	{
		global $db, $colors, $board_config, $template, $group_info, $template_path, $lang;
	
		$sql = 'SELECT count(news_id) AS num_news 
							FROM ' . NEWS_TABLE;
		if( !($res = $db->query($sql)) )
		{
			message_die(__LINE__, __FILE__, $sql);
		}
		else
		{
			$num_res = $db->fetcharray($res);
			if ($num_res['num_news'] > 0 )
			{
				$pagestart = (!request_var('pagestart', TYPE_INT)) ? 0 : request_var('pagestart', TYPE_INT);
				$add_pagination = '&mode=news_gestion';
				$pagination = generate_pagination('./' . ADMIN_PAGE . '.php', $num_res['num_news'], $board_config['news_per_page'], $pagestart, $add_pagination);
				$sql_pagination = ' LIMIT '.$pagestart.','. $board_config['news_per_page'];
	
				$sql = 'SELECT n.*, u.*, g.group_color
									FROM (' . NEWS_TABLE . ' n 
									LEFT JOIN ' . USERS_TABLE . ' u 
										ON u.user_id = n.news_user_id 
									LEFT JOIN ' . PERMISSIONS_TABLE . ' g 
										ON u.user_group = g.group_id)
								ORDER BY n.news_id DESC'.$sql_pagination;
				if( !($res = $db->query($sql)) )
				{
					message_die(__LINE__, __FILE__, $sql);
				}
				$i=0;
				while($array = $db->fetcharray($res))
				{
					$news_username[$i] = $colors->get_colors($array, $array['username']);
					$u_edit_news[$i] = ($group_info['can_edit_news']) ? '<a href="./' . ADMIN_PAGE . '.php?mode=news_gestion&mode_news=edit&news_id=' . $array['news_id'] . '"><img src="./templates/' . $template_path . '/images/' . $board_config['default_lang'] . '/icon_edit.gif" alt="' . $lang['Edit_News'] . '" title="' . $lang['Edit_News'] . '" border="0" /></a>' : '';
					$u_del_news[$i] = ($group_info['can_del_news']) ? '<a href="./' . ADMIN_PAGE . '.php?mode=news_gestion&mode_news=delete&news_id=' . $array['news_id'] . '"><img src="./templates/' . $template_path . '/images/' . $board_config['default_lang'] . '/icon_delete.gif" alt="' . $lang['Del_News'] . '" title="' . $lang['Del_News'] . '" border="0" /></a>' : '';
					$news_subject[$i] = $array['news_subject'];
					$news_image[$i] = ($array['news_image']) ? '<img src="' . $array['news_image'] . '" />' : '';
					$news_date[$i] = date('D d M G:i', $array['news_time']);
						
					$row_class[$i] = ( !($i % 2) ) ? 'row1' : 'row2';
					$news_id[$i] = $array['news_id'];
					$i++;
				}
			}
			$template->assign('U_EDIT_NEWS', $u_edit_news);
			$template->assign('U_DEL_NEWS', $u_del_news);
			$template->assign('NEWS_USERNAME', $news_username);
			$template->assign('NEWS_ID', $news_id);
			$template->assign('NEWS_SUBJECT', $news_subject);
			$template->assign('NEWS_IMAGE', $news_image);
			$template->assign('NEWS_DATE', $news_date);
			$template->assign('ROW_CLASS', $row_class);
	
			$template->assign('PAGINATION', $pagination);
			$template->assign('CAN_ADD_NEWS', $group_info['can_add_news']);
	
			$template->assign('LANG', $board_config['default_lang']);
			$template->assign('U_ADD_NEWS', './' . ADMIN_PAGE . '.php?mode=news_gestion&mode_news=create');
				
			$template->display('admin/news_gestion.tpl');
		}
	}
	
	function edit_news($news_id_send)
	{
		global $group_info, $lang, $db, $template;
	
		if ($news_id_send)
		{
			if (!$group_info['can_edit_news'])
			{
				error_message($lang['information'], $lang['cant_edit_news'], ADMIN_URL, '?mode=news_gestion');
			} else {
				$submit = request_var('submit');
			
				$news_subject = request_var('news_subject');
				$news_texte = request_var('news_texte', TYPE_HTML);
				$news_image = request_var('news_image');
				if ($submit && (!$news_subject || !$news_texte))
				{
					error_message($lang['information'], $lang['no_subject_news'], ADMIN_URL, '?mode=news_gestion&mode_news=create');
				}
				else if ($news_id_send && $submit)
				{
					$time = time();
					$sql = 'UPDATE ' . NEWS_TABLE . " SET
										news_subject = '$news_subject',
										news_texte = '$news_texte',
										news_image = '$news_image'
										WHERE news_id = '$news_id_send'";
					if( !($db->query($sql)) )
					{
						message_die(__LINE__, __FILE__, $sql);
					} else {
						error_message($lang['information'], $lang['good_edit_news'], ADMIN_URL, '?mode=news_gestion');
					}
				}
				else 
				{
					$sql = 'SELECT * FROM ' . NEWS_TABLE . '
										WHERE news_id = ' . $news_id_send;
					if( !($res = $db->query($sql)) )
					{
						message_die(__LINE__, __FILE__, $sql);
					} else {	
						while($array = $db->fetcharray($res))
						{
							$news_info = $array;
						}
						include('./includes/fckeditor/fckeditor.php');
						$FCKeditor = new FCKeditor('news_texte');
						$FCKeditor->Height = '500px';
						$FCKeditor->BasePath = './includes/fckeditor/';
						$FCKeditor->Value = $news_info['news_texte'];
						
						$template->assign('FCKEDITOR', $FCKeditor->CreateHtml());
						$template->assign('NEWS_SUBJECTS', $news_info['news_subject']);
						$template->assign('U_SEND', './' . ADMIN_PAGE . '.php?mode=news_gestion&mode_news=edit&news_id=' . $news_id_send);
					
						$template->display('admin/create_edit_news.tpl');
					}
				}
			}
		} else {
			dysplay_news();
		}
	}
	
	function create_news()
	{
		global $group_info, $lang, $userdata, $db, $template;
	
		if (!$group_info['can_add_news'])
		{
			error_message($lang['information'], $lang['cant_add_news'], ADMIN_URL, '?mode=news_gestion');
		} else {
			$submit = request_var('submit');
		
			$news_subject = request_var('news_subject');
			$news_texte = request_var('news_texte');
			$news_image = request_var('news_image');
			if ($submit && (!$news_subject || !$news_texte))
			{
				error_message($lang['information'], $lang['no_subject_news'], ADMIN_URL, '?mode=news_gestion&mode_news=create');
			} 
			else if ($submit)
			{
				$user_id = ($userdata['user_login']) ? $userdata['user_id'] : '-1';
				$time = time();
				$sql = 'INSERT INTO ' . NEWS_TABLE . "(news_subject, news_texte, news_image, news_user_id, news_time)
									VALUES ('$news_subject', '$news_texte', '$news_image', '$user_id', '$time')";
				if( !($db->query($sql)) )
				{
					message_die(__LINE__, __FILE__, $sql);
				} else {
					error_message($lang['information'], $lang['good_add_news'], ADMIN_URL, '?mode=news_gestion');
				}
			}
			else 
			{
				include('./includes/fckeditor/fckeditor.php');
				$FCKeditor = new FCKeditor('news_texte');
				$FCKeditor->Height = '500px';
				$FCKeditor->BasePath = './includes/fckeditor/';
				
				$template->assign('FCKEDITOR', $FCKeditor->CreateHtml());
				$template->assign('U_SEND', './' . ADMIN_PAGE . '.php?mode=news_gestion&mode_news=create');
				
				$template->display('admin/create_edit_news.tpl');
			}
		}
	}
	
	function delete_news($news_id_send)
	{
		global $group_info, $lang, $db;
	
		if (!$news_id_send)
		{ 
			dysplay_news();
		} else {
			if (!$group_info['can_del_news'])
			{
				error_message($lang['information'], $lang['cant_delete_news'], ADMIN_URL, '?mode=news_gestion');		
			} else {
				$sql = 'DELETE FROM ' . NEWS_TABLE . '
									WHERE news_id = ' . $news_id_send;
				if( !($res = $db->query($sql)) )
				{
					message_die(__LINE__, __FILE__, $sql);
				} else {
					error_message($lang['information'], $lang['good_del_news'], ADMIN_URL, '?mode=news_gestion');
				}
			}
		}
	}
	
	$mode_news = request_var('mode_news', TYPE_NO_HTML);
	$news_id_send = request_var('news_id', TYPE_INT);

	switch($mode_news)
	{
		case 'delete':
			delete_news($news_id_send);
			break;
		case 'create':
			create_news();
			break;
		case 'edit':
			edit_news($news_id_send);
			break;
		default:
			dysplay_news();
			break;
	}

}
	

?>
